package com.yami.shop.consign.api.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yami.shop.bean.enums.ProdConsignStatus;
import com.yami.shop.bean.vo.ProdConfigVO;
import com.yami.shop.bean.vo.ProdConsignInfoVO;
import com.yami.shop.bean.vo.ProdConsignVO;
import com.yami.shop.common.bean.PageDTO;
import com.yami.shop.common.bean.PageVO;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.consign.common.model.ProdConfig;
import com.yami.shop.consign.common.service.ConsignRecordService;
import com.yami.shop.consign.common.service.ProdConfigService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

@RestController
@RequestMapping("/app/consign")
@Tag(name = "app-寄售商品配置")
@AllArgsConstructor
@Slf4j
public class ConsignProdController {

    private final ProdConfigService prodConfigService;
    
    private final ConsignRecordService consignRecordService;

    @GetMapping("/isConsign/prodId")
    @Operation(summary =  "是否寄售商品", description = "是否寄售商品")
    public ServerResponseEntity<Boolean> getIsConsignProd(@RequestParam Integer prodId) {
        Boolean isConsign = false;
        ProdConfig one = prodConfigService.getOne(new LambdaQueryWrapper<ProdConfig>().eq(ProdConfig::getProdId, prodId).ne(ProdConfig::getStatus, ProdConsignStatus.DELETE.getValue()));
        if (Objects.nonNull(one) && one.getStatus() == ProdConsignStatus.OPEN.getValue()) {
            isConsign = true;
        }
        return ServerResponseEntity.success(isConsign);
    }

    @GetMapping("/getByProdId")
    @Operation(summary =  "获取寄售商品详情", description = "获取寄售商品详情")
    public ServerResponseEntity<ProdConfigVO> getByProdId(@RequestParam Integer prodId) {
        ProdConfig one = prodConfigService.getOne(new LambdaQueryWrapper<ProdConfig>().eq(ProdConfig::getProdId, prodId).ne(ProdConfig::getStatus, ProdConsignStatus.DELETE.getValue()));
        ProdConfigVO prodConfigVO = BeanUtil.copyProperties(one, ProdConfigVO.class);
        ProdConfigVO priceByProdId = consignRecordService.getAvgLatestPriceByProdId(prodId);
        prodConfigVO.setAvgPrice(priceByProdId.getAvgPrice());
        prodConfigVO.setLatestPrice(priceByProdId.getLatestPrice());
        prodConfigVO.setLatestTime(priceByProdId.getLatestTime());
        return ServerResponseEntity.success(prodConfigVO);
    }

    @GetMapping("/ua/getAppProdList")
    @Operation(summary =  "分页获取寄售商品主页", description = "分页获取寄售商品主页")
//    @Encrypt
    public ServerResponseEntity<PageVO<ProdConsignVO>> getAppProdList(PageDTO pageDTO , @RequestParam Integer type, @Param("prodName") String prodName, @Param("seriesId") Integer seriesId) {
        PageVO<ProdConsignVO> appProdList = prodConfigService.getAppProdList(pageDTO, type, prodName, seriesId);
        return ServerResponseEntity.success(appProdList);
    }

    @GetMapping("/getAppProdInfoList")
    @Operation(summary =  "分页获取寄售商品信息列表", description = "分页获取寄售商品信息列表")
//    @Limit
//    @Encrypt
    public ServerResponseEntity<PageVO<ProdConsignInfoVO>> getAppProdInfoList(PageDTO pageDTO , @RequestParam Integer type, @RequestParam Integer prodId) {
        if(type == null){
            type =0;
        }
        PageVO<ProdConsignInfoVO> appProdList = prodConfigService.getAppProdInfoList(pageDTO, type,prodId);
        return ServerResponseEntity.success(appProdList);
    }
    
    
    
    
    
}
